package org.example.express.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.express.mapper.ExpressActivityMessageMapper;
import org.example.express.mapper.ExpressDispatchMessageMapper;
import org.example.express.mapper.ExpressListMapper;
import org.example.express.mapper.ExpressSystemMessageMapper;
import org.example.express.service.MessageService;
import org.example.model.common.Result;
import org.example.model.domain.express.ExpressActivityMessage;
import org.example.model.domain.express.ExpressDispatchMessage;
import org.example.model.domain.express.ExpressList;
import org.example.model.domain.express.ExpressSystemMessage;
import org.example.model.domain.feeback.MessageStatus;
import org.example.model.h5.LastMessageVo;
import org.example.model.h5.MessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Service
@Slf4j
public class MessageServiceImpl implements MessageService {
    @Autowired
    ExpressSystemMessageMapper expressSystemMessageMapper;
    @Autowired
    ExpressActivityMessageMapper expressActivityMessageMapper;
    @Autowired
    ExpressDispatchMessageMapper expressDispatchMessageMapper;
    @Autowired
    ExpressListMapper expressListMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    //    EXPRESS:MESSAGE:STATUS
    @Override
    public MessageVo getMessage(Long registerIp) {


        long l = System.currentTimeMillis();
        log.info("2获取消息耗时：{}", l);
        MessageVo messageVo = new MessageVo();
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            messageVo.setExpressSystemMessage(expressSystemMessageMapper.selectByRegisterIp(registerIp));
        });
        CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {
            ExpressList expressList = expressListMapper.selectByRegisterIp(registerIp);
            messageVo.setExpressDispatchMessageList(expressDispatchMessageMapper.selectById(Long.valueOf(expressList.getId())));
        });

        CompletableFuture<Void> voidCompletableFuture2 = CompletableFuture.runAsync(() -> {
            messageVo.setExpressActivityMessage(expressActivityMessageMapper.selectAll());
        });

        CompletableFuture.allOf(voidCompletableFuture, voidCompletableFuture1, voidCompletableFuture2).join();


        log.info("2获取消息耗时：{}", System.currentTimeMillis() - l);

        return messageVo;

    }

    /*统计未读数量*/
    @Override
    public ArrayList<Integer> countUnread(Long registerIp) {
        long l = System.currentTimeMillis();
        log.info("3取消息耗时：{}", l);
        ArrayList<Integer> arrayList = new ArrayList<>();

        Integer unread1 = expressSystemMessageMapper.selectUnread(registerIp);
        arrayList.add(unread1);
        ExpressList expressList = expressListMapper.selectByRegisterIp(registerIp);
        Integer unread2 = expressDispatchMessageMapper.selectUnread(Long.valueOf(expressList.getId()));
        arrayList.add(unread2);
        Integer unread3 = expressActivityMessageMapper.selectUnread();
        arrayList.add(unread3);

        log.info("3获取消息耗时：{}", System.currentTimeMillis() - l);
        return arrayList;
    }

    @Override
    public Result updateSysMessage(Long registerIp) {
        try {
            expressSystemMessageMapper.updateSystemMessageIsRead(registerIp);
            return Result.build(200, "更新成功");
        } catch (Exception e) {
            return Result.build(500, "更新失败");
        }
    }

    @Override
    public Result updateDispatchMessage(Integer id) {
        try {
            expressDispatchMessageMapper.updateDispatchMessageIsRead(id);
            return Result.build(200, "更新成功");
        } catch (Exception e) {
            return Result.build(500, "更新失败");
        }
    }

    @Override
    public Result updateActivityMessage() {
        try {
            expressActivityMessageMapper.updateActivityMessageIsRead();
            return Result.build(200, "更新成功");
        } catch (Exception e) {
            return Result.build(500, "更新失败");
        }
    }

    @Override
    public Result getRedis() {

        //得到所有
        List<MessageStatus> rows = null;
        ListOperations listOperations = redisTemplate.opsForList();
        rows = listOperations.range("EXPRESS:MESSAGE:STATUS", 0, -1);
        return Result.build(200, "获取成功", rows);
    }

    @Override
    public LastMessageVo lastMessage(Long registerIp) {

        LastMessageVo lastMessageVo = new LastMessageVo();

        ExpressSystemMessage expressSystemMessage = expressSystemMessageMapper.selectLastOntByRegisterIp(registerIp);
        if (expressSystemMessage != null) {
            lastMessageVo.setExpressSystemMessage(expressSystemMessage);
        }

        ExpressList expressList = expressListMapper.selectByRegisterIp(registerIp);
        if (expressDispatchMessageMapper.selectLastOnt(Long.valueOf(expressList.getId())) != null) {
            lastMessageVo.setExpressDispatchMessage(expressDispatchMessageMapper.selectLastOnt(Long.valueOf(expressList.getId())));
        }

        if (expressActivityMessageMapper.selectLastOnt() != null) {
            lastMessageVo.setExpressActivityMessage(expressActivityMessageMapper.selectLastOnt());
        }

        return lastMessageVo;
    }

    @Override
    public ExpressActivityMessage getLastOneActivityMessage(Long registerIp) {
        return expressActivityMessageMapper.selectLastOnt();
    }

    @Override
    public ExpressSystemMessage getLastOneSysMessage(Long registerIp) {
        return expressSystemMessageMapper.selectLastOntByRegisterIp(registerIp);
    }

    @Override
    public ExpressDispatchMessage getLastOneDisMessage(Long registerIp) {
        ExpressList expressList = expressListMapper.selectByRegisterIp(registerIp);
        return expressDispatchMessageMapper.selectLastOnt(Long.valueOf(expressList.getId()));
    }

    @Override
    public List<ExpressActivityMessage> getActivityMessage() {
        return expressActivityMessageMapper.selectAll();
    }

    @Override
    public List<ExpressSystemMessage> getSysMessage(Long registerIp) {
        return expressSystemMessageMapper.selectByRegisterIp(registerIp);
    }

    @Override
    public List<ExpressDispatchMessage> getLastMessage(Long registerIp) {
        ExpressList expressList = expressListMapper.selectByRegisterIp(registerIp);
        return expressDispatchMessageMapper.selectById(Long.valueOf(expressList.getId()));
    }

}
